package cn.fl.ref.fncprjeaplysettle.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.fncprjeaplysettle.service.IFncPrjEaplySettleService;
import cn.fl.ref.fncprjeaplysettle.transform.FncPrjEaplySettleVOTransform;
import cn.fl.ref.fncprjeaplysettle.transform.FncPrjPayTbInfoVOTransform;
import cn.fl.ref.fncprjeaplysettle.transform.SettlePrjDetailVOTransform;
import cn.fl.ref.fncprjeaplysettle.vo.FncPrjEaplySettleVO;
import cn.fl.ref.fncprjeaplysettle.vo.FncPrjPayTbInfoVO;
import cn.fl.ref.fncprjeaplysettle.vo.SettlePrjDetailVO;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.ref.fncprjeaplysettle.condition.FncPrjEaplySettleQueryCondition;
import fl.ref.fncprjeaplysettle.dto.FncPrjEaplySettleDTO;
import fl.ref.fncprjeaplysettle.dto.FncPrjPayTbInfoDTO;
import fl.ref.fncprjeaplysettle.dto.SettlePrjDetailDTO;
import fl.ref.fncprjeaplysettle.service.IFncPrjEaplySettleApiService;

/**
 * @param <E>
 * @Description: 融资项目首付款信息
 */
@Service
public class FncPrjEaplySettleServiceImpl implements IFncPrjEaplySettleService {

	private static final Logger logger = LoggerFactory.getLogger(IFncPrjEaplySettleService.class);

	@Autowired
	private IFncPrjEaplySettleApiService fncPrjEaplySettleApiService;

	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public FncPrjEaplySettleVO selectById(Long id) throws BaseException {
		if (StringUtils.isEmpty(id)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		FncPrjEaplySettleDTO dto = this.fncPrjEaplySettleApiService.selectById(id);
		return FncPrjEaplySettleVOTransform.toVO(dto);
	}

	@Override
	public Long add(FncPrjEaplySettleVO entity) throws BaseException {
		if (null == entity) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		// 系统日志-有用户的情况下
		BaseLogUtils.info(logger, "add", "新增FncPrjEaplySettle数据.", CurrentThreadContext.getCurrentUserName());

		// 系统日志-无用户的情况下
		BaseLogUtils.info(logger, "add", "新增FncPrjEaplySettle数据.");
		return this.fncPrjEaplySettleApiService.add(FncPrjEaplySettleVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		if (StringUtils.isEmpty(id)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.fncPrjEaplySettleApiService.removeById(id);
	}

	@Override
	public boolean modify(FncPrjEaplySettleVO entity) throws BaseException {
		if (null == entity) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.fncPrjEaplySettleApiService.modify(FncPrjEaplySettleVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		if (null == page)
			page = new Pager();
		return this.fncPrjEaplySettleApiService.searchListPage(page);
	}

	@Override
	public List<FncPrjEaplySettleVO> searchList(FncPrjEaplySettleQueryCondition condition) {
		return FncPrjEaplySettleVOTransform.toVOList(this.fncPrjEaplySettleApiService.searchList(condition));
	}
	
	@Override
	public boolean modifySettlePrjDetail(SettlePrjDetailVO entity) throws BaseException {
		if (null == entity) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.fncPrjEaplySettleApiService.modifySettlePrjDetail(SettlePrjDetailVOTransform.toDTO(entity));
	}

	@Override
	public SettlePrjDetailVO selectByFncPrjId(Long fncPrjId) throws BaseException {
		if (StringUtils.isEmpty(fncPrjId)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		SettlePrjDetailDTO dto = this.fncPrjEaplySettleApiService.selectByFncPrjId(fncPrjId);
		return SettlePrjDetailVOTransform.toVO(dto);
	}

	@Override
	public Pager selectSettlePrjDetailListPage(Pager page) throws BaseException {
		if (null == page)
			page = new Pager();
		return this.fncPrjEaplySettleApiService.selectSettlePrjDetailListPage(page);
	}
	
	@Override
	public Pager selectEaplySettlePrjListPage(Pager page) throws BaseException {
		if (null == page)
			page = new Pager();
		return this.fncPrjEaplySettleApiService.selectEaplySettlePrjListPage(page);
	}

	@Override
	public DataResultDTO addNormalSettleFlowStart(FlowStartParamsDTO flowStartParamsDTO) throws BaseException {
		if (StringUtils.isEmpty(flowStartParamsDTO.getCrtUsrId()) || StringUtils.isEmpty(flowStartParamsDTO.getBusinessKey())) {
			BaseLogUtils.info(logger, "addFlowStart", "用户ID和业务数据主键必须要存在!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "用户ID和业务数据主键必须要存在!");
		}
		return this.fncPrjEaplySettleApiService.addNormalSettleFlowStart(flowStartParamsDTO);
	}


	@Override
	public List<FncPrjPayTbInfoVO> selectPrjPayTbInfoList(Long prjId) throws BaseException {
		if (null == prjId||prjId==0L) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		List<FncPrjPayTbInfoDTO> entityListDTO=this.fncPrjEaplySettleApiService.selectPrjPayTbInfoList(prjId);
		List<FncPrjPayTbInfoVO> entityListVO=FncPrjPayTbInfoVOTransform.toVOList(entityListDTO);
		return entityListVO;
	}

	@Override
	public SettlePrjDetailVO selectPrjInfoByPrjId(Long prjId) throws BaseException {
		if (null == prjId||prjId==0L) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		SettlePrjDetailDTO entity=this.fncPrjEaplySettleApiService.selectPrjInfoByPrjId(prjId);
		SettlePrjDetailVO entityVO=SettlePrjDetailVOTransform.toVO(entity);
		return entityVO;
	}
	
	@Override
	public SettlePrjDetailVO selectEaplyInfoById(Long id) throws BaseException {
		if (null == id||id==0L) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		SettlePrjDetailDTO entity=this.fncPrjEaplySettleApiService.selectEaplyInfoById(id);
		SettlePrjDetailVO entityVO=SettlePrjDetailVOTransform.toVO(entity);
		return entityVO;
	}
	
	@Override
	public SettlePrjDetailVO selectEaplyInfoByPrjId(Long id) throws BaseException {
		if (null == id||id==0L) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		SettlePrjDetailDTO entity=this.fncPrjEaplySettleApiService.selectEaplyInfoByPrjId(id);
		SettlePrjDetailVO entityVO=SettlePrjDetailVOTransform.toVO(entity);
		return entityVO;
	}
	
	@Override
	public SettlePrjDetailVO selectOnlyPrjInfoByPrjId(Long id) throws BaseException {
		if (null == id||id==0L) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		SettlePrjDetailDTO entity=this.fncPrjEaplySettleApiService.selectOnlyPrjInfoByPrjId(id);
		SettlePrjDetailVO entityVO=SettlePrjDetailVOTransform.toVO(entity);
		return entityVO;
	}
	
	@Override
	public DataResultDTO addPrjEaplySettleFlowStart(FlowStartParamsDTO flowStartParamsDTO) throws BaseException{
		if(StringUtils.isEmpty(flowStartParamsDTO.getCrtUsrId()) || StringUtils.isEmpty(flowStartParamsDTO.getBusinessKey())){
			BaseLogUtils.info(logger, "addPrjEaplySettleFlowStart", "用户ID和业务数据主键必须要存在!",CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "用户ID和业务数据主键必须要存在!");
		}
		return this.fncPrjEaplySettleApiService.addPrjEaplySettleFlowStart(flowStartParamsDTO);
	}
	
	@Override
	public ArrayList selectPrjEsPdByPrjId(Long prjId) throws BaseException{
		ArrayList esPd=this.fncPrjEaplySettleApiService.selectPrjEsPdByPrjId(prjId);
		return esPd;
	}
}
